﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms
{
    public partial class frmChipViewer : Form
    {
        #region Private fields

        private DisciplineListEntity discipline;
        private int selectedCheckPointId;

        #endregion

        #region Ctor

        public frmChipViewer(DisciplineListEntity discipline)
        {
            InitializeComponent();
            
            this.discipline = discipline;
            this.Icon = Properties.Resources.ChipViewer64;
        }

        #endregion

        #region Event handlers

        // Form Load
        private void frmChipViewer_Load(object sender, EventArgs e)
        {
            try
            {
                DatabaseHelper.FillCheckPointsByDisciplineListId(this.aDbDataSet.CheckPoints, discipline.ID);
                lblTitle.Text = discipline.Name;

                if (aDbDataSet.CheckPoints.Rows.Count > 0)
                {
                    RefreshData();
                    tmrRefresh.Enabled = true;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("V aplikácii nastala chyba!" + Environment.NewLine + "Detail: " +  ex.Message, "ChipViewer Load");
            }
        }

        // Zmena check-pointu
        private void checkPointsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (checkPointsDataGridView.CurrentRow != null)
            {
                try
                {
                    DataRowView drv = (DataRowView)checkPointsDataGridView.CurrentRow.DataBoundItem;
                    this.selectedCheckPointId = Convert.ToInt32(drv["ID"].ToString());
                }
                catch
                {
                    MessageBox.Show("Zvolený check-point nie je správne zadefinovaný.", "Chip Viewer", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        // Timer Tick
        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            this.RefreshData();
        }

        // Form closing
        private void frmChipViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            tmrRefresh.Enabled = false;
            tmrRefresh.Dispose();
        }
        
        // Auto-refresh
        private void chkAutoRefresh_CheckedChanged(object sender, EventArgs e)
        {
            tmrRefresh.Enabled = chkAutoRefresh.Checked;
            if (chkAutoRefresh.Checked)
                tsslStatus.Text = "Auto refresh: ON";
            else
                tsslStatus.Text = "Auto refresh: OFF";
        }

        #endregion

        #region Private methods

        // Refresh data
        private void RefreshData()
        {
            this.lbChips1.Items.Clear();
            this.lbChips2.Items.Clear();
            this.lbChips3.Items.Clear();

            if (this.checkPointsDataGridView.Rows.Count == 0)
                return;

            this.discipline = Helpers.DisciplineListHelper.ConvertToEntity(DatabaseHelper.GetDisciplineListById(discipline.ID).Rows[0]);

            try
            {
                DataTable dtChipData = DatabaseHelper.GetChipDataByCheckPointId(this.selectedCheckPointId);
                int rowCount = dtChipData.Rows.Count;
                string line;

                // 01 - 30 records to 1st ListBox
                for (int i = rowCount - 1; i > rowCount - 31; i--)
                {
                    if (i > -1)
                    {
                        line = this.GetLine(i, dtChipData.Rows[i]);
                        this.lbChips1.Items.Add(line);
                    }
                }

                // 31 - 60 records to 1st ListBox
                for (int i = rowCount - 31; i > rowCount - 61; i--)
                {
                    if (i > -1)
                    {
                        line = this.GetLine(i, dtChipData.Rows[i]);
                        this.lbChips2.Items.Add(line);
                    }
                }

                // 61 - 90 records to 1st ListBox
                for (int i = rowCount - 61; i > rowCount - 91; i--)
                {
                    if (i > -1)
                    {
                        line = this.GetLine(i, dtChipData.Rows[i]);
                        this.lbChips3.Items.Add(line);
                    }
                }

                this.tsslTime.Text = "Obnovené: " + DateTime.Now.ToLongTimeString();
            }
            catch (Exception ex)
            {
                this.tmrRefresh.Enabled = false;
                MessageBox.Show(ex.Message, "Chip Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Get line for ListBox
        private string GetLine(int index, DataRow drChipData)
        {
            string myLine = (index + 1).ToString().PadLeft(2, '0') + ". (" + drChipData["StartNumber"].ToString().PadLeft(3, '0') + ") ";

            DateTime chipDateTime = Convert.ToDateTime(drChipData["ChipDateTime"]);
            chipDateTime = RoundingHelper.RoundUpDateTime(chipDateTime, discipline.ResultCode);
            myLine += PrintHelper.PrintResult2(chipDateTime, discipline.ResultCode);

            myLine += " - ";
            myLine += drChipData["FirstName"].ToString() + " ";
            myLine += drChipData["Surname"].ToString() + " ";

            return myLine;
        }

        #endregion



    }
}
